/*
 *  Copyright 2005 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.action.questionnaire.answerOption;

import org.apache.struts.tiles.ControllerSupport;
import org.apache.struts.tiles.ComponentContext;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletContext;

import com.blandware.atleap.webapp.util.questionnaire.QuestionnaireModuleWebConstants;
import com.blandware.atleap.service.questionnaire.QuestionnaireManager;
import com.blandware.atleap.common.QuestionnaireModuleConstants;

/**
 * <p>Answer option list controller.</p>
 * <p><a href="AnswerOptionListController.java.html"><i>View source</i></a></p>
 *
 * @author Roman Puchkovskiy <a href="mailto:roman.puchkovskiy@blandware.com">
 *         &lt;roman.puchkovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.2 $ $Date: 2006/10/31 11:55:55 $
 */
public class AnswerOptionListController extends ControllerSupport {
	/**
	 * Puts some values into request.
	 *
	 * @param tilesContext   Current tile context
	 * @param request        Current request
	 * @param response       Current response
	 * @param servletContext Current Servlet Context
	 */
	public void execute(ComponentContext tilesContext,
                        HttpServletRequest request,
                        HttpServletResponse response,
                        ServletContext servletContext)
	        throws Exception {

        HttpSession session = request.getSession();
		Long questionId = (Long) session.getAttribute(QuestionnaireModuleWebConstants.QUESTION_ID_KEY);
		if ( questionId != null ) {
			ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
			QuestionnaireManager questionnaireManager = (QuestionnaireManager) applicationContext.getBean(QuestionnaireModuleConstants.QUESTIONNAIRE_MANAGER_BEAN);
            Integer[] positions = questionnaireManager.getAnswerOptionFirstLastOccupiedPositions(questionId);
            Integer minPosition = positions[0];
            Integer maxPosition = positions[1];

            request.setAttribute("minPosition", minPosition);
            request.setAttribute("maxPosition", maxPosition);
		}
	}
}
